#include<stdio.h>
#include<stdlib.h>
#include "golf.h"
#include "util.h"

void write_master_file(char fileName[], int nteams, int num_players)
{
    int i, j;
    FILE *fp;
    int max_name_len;

    /* open (create) the master data file */
    if ((fp = fopen(fileName, "w")) == NULL) {
        perror("Error opening Master file");
        exit(-1);
    }

    /* write the number of teams and the number of players as a header for the master
       file reader function  */
    if ((fprintf(fp, " %2d %2d \n", nteams, num_players)) == -1) {
        printf("Error Master File - Writing header \n");
        exit(-1);
    }

    /* the output format insures all player record line up in the master file.  This
       should help spot errors   */

    /* find the longest player name and use the length to format the output */
    max_name_len = findLongestName();

    /* write all player records.  Output is ASCII  */
    for (i = FIRST_PLAYER; i <= num_players; i++) {
        if ((fprintf
             (fp, "%2d %-*s %1c %4.1f ", players[i].id_number,
              max_name_len, players[i].name, players[i].initial,
              players[i].curr_hcp)) == -1) {
            printf
                ("Error Master File - Writing player record number %d - id,name,and initial",
                 i);
            exit(-1);
        }

        /* now print out the players sub history  */
        for (j = 0; j < HCP_WINDOW_LEN; j++)
            if ((fprintf(fp, "%4.1f ", players[i].ESCAdjustedScores[j])) == -1) {
                printf
                    ("Error Master File - Writing player record %d hcp adjusted  %d \n",
                     i, j);
                exit(-1);
            }

        /* now print out the players sub history  */
        for (j = 0; j < MAX_RNDS; j++)
            if ((fprintf(fp, "%2d ", players[i].subNumber[j])) == -1) {
                printf
                    ("Error Master File - Writing player record %d sub history %d \n",
                     i, j);
                exit(-1);
            }

        /* write out the scores for each round played */
        for (j = 0; j < MAX_RNDS; j++)
            if ((fprintf(fp, "%2d ", players[i].all_scores[j])) == -1) {
                printf
                    ("Error Master File - Writing record %d scores %d \n",
                     i, j);
                exit(-1);
            }

        /* write out the net scores for each round played */
        for (j = 0; j < MAX_RNDS; j++)
            if ((fprintf(fp, "%2d ", players[i].netScores[j])) == -1) {
                printf
                    ("Error Master File - Writing record %d net scores %d \n",
                     i, j);
                exit(-1);
            }

        /* write out the putts for each round played */
        for (j = 0; j < MAX_RNDS; j++)
            if ((fprintf(fp, "%2d ", players[i].putts[j])) == -1) {
                printf
                    ("Error Master File - Writing record %d points won %d \n",
                     i, j);
                exit(-1);
            }

        /* write out the points the player has won   */
        for (j = 0; j < MAX_RNDS; j++)
            if ((fprintf(fp, "%2d ", players[i].matchPointsWon[j]))
                == -1) {
                printf
                    ("Error Master File - Writing record %d points won %d \n",
                     i, j);
                exit(-1);
            }

        /* write out which nine holes the player played for each match */
        for (j = 0; j < MAX_RNDS; j++)
            if ((fprintf(fp, " %1c ", players[i].whichNinePlayed[j])) == -1) {
                printf
                    ("Error Master File - Writing record %d whichNinePlayed %d \n",
                     i, j);
                exit(-1);
            }

        /* write the record num of holes played per round  */
        for (j = 0; j < MAX_RNDS; j++)
            if ((fprintf(fp, "%2d ", players[i].numHolesPlayed[j]))
                == -1) {
                printf
                    ("Error Master File - Writing record %d numHolesPlayed %d \n",
                     i, j);
                exit(-1);
            }

        fprintf(fp, "\n");      /* write end of record */

    }                           /* end of loop over all player records */

    return;
}                               /* end of function write_master_file */
